<template>
	<view class="introduce">
		<view class="section">
			<view class="title">
				<text>企业地址</text>
				<text class="text-btn" @tap="selectAddress">获取地址</text>
			</view>
			<!-- <textarea v-model="address.address" readonly maxlength="200" cols="30" rows="10" placeholder="请获取地址"></textarea> -->
			<view class="address">{{address.address}}</view>
		</view>
		<view class="btn-box">
			<view class="btn" @tap="submit">保存</view>
		</view>
	</view>
</template>

<script>
	import http from '@/utils/http.js'
	export default {
		data() {
			return {
			  title: 'Hello',
			  value: 1,
			  userInfo: {},
			  info: {},
			  address: {}
			}
		},
		onLoad() {
			this.getData()
		},
		methods: {
			getMapLocation() {
			    uni.chooseLocation({
			        success: (res) => {
			            console.log(res);
			            // 处理返回的位置信息
						console.log('位置名称：' + res.name);
						console.log('详细地址：' + res.address);
						console.log('纬度：' + res.latitude);
						console.log('经度：' + res.longitude);
						this.info.address = {
							name: res.name,
							address: res.address,
							latitude: res.latitude,
							longitude: res.longitude
						}
						this.address = {
							name: res.name,
							address: res.address,
							latitude: res.latitude,
							longitude: res.longitude
						}
			        },
			        fail: () => {
			            // 处理失败情况，可能需要再次请求授权
			        }
			    });
			},
			selectAddress() {
				// uni.getSetting({
				//     success: (res) => {
				//         if (!res.authSetting['scope.userLocation']) {
				//             uni.showModal({
				//                 title: "是否授权当前位置",
				//                 content: "需要获取您的地理位置，请确认授权，否则地图功能将无法使用",
				//                 success: (tip) => {
				//                     if (tip.confirm) {
				//                         uni.openSetting({});
				//                     }
				//                 }
				//             });
				//         } else {
				//             this.getMapLocation();
				//         }
				//     }
				// })
				this.getMapLocation();
			},
			submit() {
				if (!(this.address && this.address.name)) {
					uni.showToast({
						title: '请填写地址',
						icon: 'none'
					});
					return false;
				}
				http({
					url: 'card/user-profile/company/update',
					method: 'put',
					data: {
						// ...this.info,
						address: JSON.stringify(this.address)
					}
				}).then(res => {				
					if(res.code === 0) {
						uni.showToast({
							title: '保存成功',
							icon: "success"
						});
					}
				})
			},
			getData() {
				http({
					url: `card/user-profile/get`
				}).then(res => {				
					if(res.code === 0) {
						const data = res.data
						try{
							this.address = (data.companyProfileDO.address ? JSON.parse(data.companyProfileDO.address) : {})
						}catch(e){
							//TODO handle the exception
						}
						this.info = data.companyProfileDO
					}
				})		
			}
		},
	}
</script>

<style lang="scss" scoped>
	.introduce {
		background: #F0F0F4;
		min-height: 100vh;
		padding: 20rpx 20rpx calc(140rpx + var(--safe-area-inset-bottom));
		box-sizing: border-box;
		font-family: PingFang SC;
		.section {
			// height: 908rpx;
			height: 508rpx;
			background: #FFFFFF;
			border-radius: 40rpx;
			padding: 40rpx;
			box-sizing: border-box;
			.title {
				font-weight: 500;
				font-size: 28rpx;
				color: #333333;
				margin-bottom: 25rpx;
				display: flex;
				align-items: center;
				justify-content: space-between;
				.text-btn {
					font-weight: 500;
					font-size: 28rpx;
					color: #4B73FF;
				}
			}
			.address {
				font-weight: 500;
				font-size: 28rpx;
				// color: #CCCCCC;
				color: #333333;
				border-radius: 10rpx;
				border: 1rpx solid #EEEEEE;
				width: 100%;
				// height: 773rpx;
				height: 373rpx;
				box-sizing: border-box;
				padding: 28rpx 24rpx;
				&::placeholder {
					color: #CCCCCC;
				}
			}
		}
		.tips {
			font-weight: 500;
			font-size: 22rpx;
			color: #999999;
			line-height: 40rpx;
			margin: 20rpx 0 24rpx;
		}
		.img-box {
			display: flex;
			align-items: center;
			image {
				width: 162rpx;
				height: 162rpx;
				border-radius: 20rpx;
				margin-right: 20rpx;
			}
			.add-btn {
				width: 162rpx;
				height: 162rpx;
				background: #E6ECFF;
				border-radius: 20rpx;
				border: 1rpx dashed #4B73FF;
				display: flex;
				align-items: center;
				justify-content: center;
				font-weight: 500;
				font-size: 28rpx;
				color: #4B73FF;
			}
		}
		.btn-box {
			position: fixed;
			bottom: 0;
			left: 0;
			width: 100%;
			padding: 20rpx 20rpx calc(20rpx + var(--safe-area-inset-bottom));
			box-sizing: border-box;
			background-color: #FFFFFF;
			.btn {
				height: 90rpx;
				line-height: 90rpx;
				text-align: center;
				background: #4B73FF;
				border-radius: 20rpx;
				font-weight: bold;
				font-size: 28rpx;
				color: #FFFFFF;
				// margin-top: 42rpx;
			}
		}
	}
</style>